package com.imut.lagain.controller;

import com.imut.lagain.entity.MoodStealRecord;
import com.imut.lagain.entity.MoodThief;
import com.imut.lagain.entity.MemoryEntry;
import com.imut.lagain.service.IMoodThiefGameService;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 心情小偷游戏控制器
 */

@RestController
@RequestMapping("/api/mood-thief-game")
@RequiredArgsConstructor
public class MoodThiefGameController {
    private static final Logger log = LoggerFactory.getLogger(MoodThiefGameController.class);

    
    private final IMoodThiefGameService moodThiefGameService;
    
    /**
     * 执行偷取操作
     * @param request 偷取请求
     * @return 偷取结果
     */
    @PostMapping("/steal")
    public ResponseEntity<Map<String, Object>> performSteal(@RequestBody StealRequest request) {
        Map<String, Object> response = new HashMap<>();
        
        try {
            Map<String, Object> result = moodThiefGameService.performSteal(
                    request.getThiefUserId(), 
                    request.getVictimUserId(), 
                    request.getThiefId()
            );
            
            response.put("success", result.get("success"));
            response.put("message", result.get("message"));
            response.put("data", result);
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("执行偷取操作失败", e);
            response.put("success", false);
            response.put("message", "偷取操作失败: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }
    
    /**
     * 尝试追回被偷的心情
     * @param request 追回请求
     * @return 追回结果
     */
    @PostMapping("/recovery")
    public ResponseEntity<Map<String, Object>> attemptRecovery(@RequestBody RecoveryRequest request) {
        Map<String, Object> response = new HashMap<>();
        
        try {
            Map<String, Object> result = moodThiefGameService.attemptRecovery(
                    request.getStealRecordId(), 
                    request.getVictimUserId()
            );
            
            response.put("success", result.get("success"));
            response.put("message", result.get("message"));
            response.put("data", result);
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("尝试追回失败", e);
            response.put("success", false);
            response.put("message", "追回操作失败: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }
    
    /**
     * 获取可偷取的记忆条目
     * @param victimUserId 被偷者ID
     * @param thiefUserId 偷取者ID
     * @return 可偷取的条目列表
     */
    @GetMapping("/stealable-entries")
    public ResponseEntity<Map<String, Object>> getStealableEntries(
            @RequestParam Long victimUserId,
            @RequestParam Long thiefUserId) {
        Map<String, Object> response = new HashMap<>();
        
        try {
            List<MemoryEntry> entries = moodThiefGameService.getStealableEntries(victimUserId, thiefUserId);
            
            response.put("success", true);
            response.put("message", "获取可偷取条目成功");
            response.put("data", entries);
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("获取可偷取条目失败", e);
            response.put("success", false);
            response.put("message", "获取可偷取条目失败: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }
    
    /**
     * 获取随机心情小偷
     * @return 随机心情小偷
     */
    @GetMapping("/random-thief")
    public ResponseEntity<Map<String, Object>> getRandomThief() {
        Map<String, Object> response = new HashMap<>();
        
        try {
            MoodThief thief = moodThiefGameService.getRandomThief();
            
            if (thief != null) {
                response.put("success", true);
                response.put("message", "获取随机心情小偷成功");
                response.put("data", thief);
            } else {
                response.put("success", false);
                response.put("message", "暂无可用的心情小偷");
            }
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("获取随机心情小偷失败", e);
            response.put("success", false);
            response.put("message", "获取随机心情小偷失败: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }
    
    /**
     * 检查用户是否可以进行偷取操作
     * @param thiefUserId 偷取者ID
     * @param victimUserId 被偷者ID
     * @return 是否可以偷取
     */
    @GetMapping("/can-steal")
    public ResponseEntity<Map<String, Object>> canSteal(
            @RequestParam Long thiefUserId,
            @RequestParam Long victimUserId) {
        Map<String, Object> response = new HashMap<>();
        
        try {
            boolean canSteal = moodThiefGameService.canSteal(thiefUserId, victimUserId);
            
            response.put("success", true);
            response.put("message", "检查偷取权限成功");
            response.put("data", Map.of("canSteal", canSteal));
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("检查偷取权限失败", e);
            response.put("success", false);
            response.put("message", "检查偷取权限失败: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }
    
    /**
     * 获取用户的偷取记录
     * @param userId 用户ID
     * @param isThief 是否作为偷取者
     * @return 偷取记录列表
     */
    @GetMapping("/steal-records")
    public ResponseEntity<Map<String, Object>> getUserStealRecords(
            @RequestParam Long userId,
            @RequestParam(defaultValue = "true") boolean isThief) {
        Map<String, Object> response = new HashMap<>();
        
        try {
            List<MoodStealRecord> records = moodThiefGameService.getUserStealRecords(userId, isThief);
            
            response.put("success", true);
            response.put("message", "获取偷取记录成功");
            response.put("data", records);
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("获取偷取记录失败", e);
            response.put("success", false);
            response.put("message", "获取偷取记录失败: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }
    
    /**
     * 获取偷取统计信息
     * @param userId 用户ID
     * @return 统计信息
     */
    @GetMapping("/statistics")
    public ResponseEntity<Map<String, Object>> getStealStatistics(@RequestParam Long userId) {
        Map<String, Object> response = new HashMap<>();
        
        try {
            Map<String, Object> statistics = moodThiefGameService.getStealStatistics(userId);
            
            response.put("success", true);
            response.put("message", "获取统计信息成功");
            response.put("data", statistics);
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("获取统计信息失败", e);
            response.put("success", false);
            response.put("message", "获取统计信息失败: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }
    
    /**
     * 获取幽默文案
     * @param moodType 心情类型
     * @param thiefType 小偷类型
     * @return 幽默文案
     */
    @GetMapping("/humor-message")
    public ResponseEntity<Map<String, Object>> getHumorMessage(
            @RequestParam String moodType,
            @RequestParam String thiefType) {
        Map<String, Object> response = new HashMap<>();
        
        try {
            String message = moodThiefGameService.getHumorMessage(moodType, thiefType);
            
            response.put("success", true);
            response.put("message", "获取幽默文案成功");
            response.put("data", Map.of("humorMessage", message));
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("获取幽默文案失败", e);
            response.put("success", false);
            response.put("message", "获取幽默文案失败: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }
    
    /**
     * 偷取请求体
     */
    public static class StealRequest {
        private Long thiefUserId;
        private Long victimUserId;
        private Long thiefId;
        
        public Long getThiefUserId() {
            return thiefUserId;
        }
        
        public void setThiefUserId(Long thiefUserId) {
            this.thiefUserId = thiefUserId;
        }
        
        public Long getVictimUserId() {
            return victimUserId;
        }
        
        public void setVictimUserId(Long victimUserId) {
            this.victimUserId = victimUserId;
        }
        
        public Long getThiefId() {
            return thiefId;
        }
        
        public void setThiefId(Long thiefId) {
            this.thiefId = thiefId;
        }
    }
    
    /**
     * 追回请求体
     */
    public static class RecoveryRequest {
        private Long stealRecordId;
        private Long victimUserId;
        
        public Long getStealRecordId() {
            return stealRecordId;
        }
        
        public void setStealRecordId(Long stealRecordId) {
            this.stealRecordId = stealRecordId;
        }
        
        public Long getVictimUserId() {
            return victimUserId;
        }
        
        public void setVictimUserId(Long victimUserId) {
            this.victimUserId = victimUserId;
        }
    }
}

